package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.MarketAdminService;
import com.cskaoyan.market.service.impl.MarketAdminServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: AdminSystemController
 * Package: com.cskaoyan.market.controller
 * Date: 2023/5/6 - 17:49
 * Description:
 */
@WebServlet("/admin/admin/*")
public class AdminAdminController extends HttpServlet {
    MarketAdminService marketAdminService = new MarketAdminServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/admin/", "");
        switch (replace) {
            case "list":
                list(req, resp);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/admin/", "");
        switch (replace) {
            case "update":
                update(req, resp);
                break;
            case "create":
                create(req, resp);
                break;
            case "delete":
                delete(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userName = req.getParameter("username");
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        if(StringUtils.isEmpty(sort)){
            sort="id";
        }
        if(StringUtils.isEmpty(order)){
            order="desc";
        }
        if (!("asc".equals(order) || "desc".equals(order))) {
            Object o = ResponseUtil.badArgumentValue();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        List<MarketAdmin> marketAdmins = marketAdminService.list(page, limit, sort, order,userName);
        if (marketAdmins.size() == 0) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        Object okList = ResponseUtil.okList(marketAdmins);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(okList));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = getRequestBody(req);
        MarketAdmin marketAdmin = null;
        marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        if (StringUtils.isEmpty(marketAdmin.getUsername()) || StringUtils.isEmpty(marketAdmin.getPassword()) || marketAdmin.getId() == 0) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        MarketAdmin updataMarketAdmin = marketAdminService.update(marketAdmin);
        if (updataMarketAdmin == null) {
            Object o = ResponseUtil.updatedDataFailed();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        Object ok = ResponseUtil.ok(updataMarketAdmin);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = getRequestBody(req);
        MarketAdmin marketAdmin = null;
        marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        if (StringUtils.isEmpty(marketAdmin.getUsername()) || StringUtils.isEmpty(marketAdmin.getPassword())) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        List<MarketAdmin> marketAdmins = marketAdminService.create(marketAdmin);
        if (marketAdmins == null) {
            Object o = ResponseUtil.badArgumentValue();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        Object ok = ResponseUtil.ok(marketAdmins);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private static String getRequestBody(HttpServletRequest req) throws IOException {
        ServletInputStream in = req.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = in.read(bytes)) != -1) {
            out.write(bytes, 0, length);
        }
        String requestBody = out.toString("utf-8");
        return requestBody;
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = getRequestBody(req);
        MarketAdmin marketAdmin = null;
        marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        int deleteIndex = marketAdminService.delete(marketAdmin);
        if (deleteIndex != 1) {
            Object o = ResponseUtil.badArgumentValue();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        Object o = ResponseUtil.ok(deleteIndex);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }
}
